package com.zcm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.zcm.entity.address;
import com.zcm.entity.commodity;
import com.zcm.entity.favorite;
import com.zcm.entity.user;

/**
 * @author Xrh 对User进行操作的数据访问层
 */
public class UserDao extends BaceDao {
	/**
	 * @author Xrh 核查用户名密码是否正确，若无法得到查询信息，返回i=0，若能查询到信息，i=1。
	 * 
	 * 
	 */
	public List<user> CheckUser(user user) {
		String hql = "from user  where username=?and password=?";
		// 自定义hql语句，返回List判断Size
		List<user> list = session.createQuery(hql).setString(0, user.getUsername()).setString(1, user.getPassword())
				.list();

		tran.commit();
		session.close();
		factory.close();
		return list;
	}

	/**
	 * @author Xrh user表中注册用户的方法
	 * 
	 * 
	 */
	public int AddUser(user user) {
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		Transaction tran = session.beginTransaction();
		user.getUsername();
		user.getPassword();
		user.getName();
		session.save(user);
		tran.commit();
		session.close();
		factory.close();
		return 0;
	}

	/**
	 * @author Xrh 核查用户名密码是否存在，若无法得到查询信息，返回i=1，若能查询到信息，i=0。
	 * 
	 * 
	 */
	public int isExit(String username) {
		int i = 0;
		String hql = "from user where username = ? ";
		List<user> list = session.createQuery(hql).setString(0, username).list();
		if (list.size() > 0) {
			i = 1;
		} else {
		}
		System.out.println(username);
		tran.commit();
		session.close();
		factory.close();
		return i;

	}

	/**
	 * @author Xrh 付款时检查用户收货地址。
	 * 
	 */
	public List<address> SelectAddressByUserID(int userid) {
		List<address> list = new ArrayList<>();
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("select * from address where userid=?");
			pstm.setInt(1, userid);
			ResultSet rs = pstm.executeQuery();
			address a = null;
			while (rs.next()) {
				a = new address();
				a.setAlabel(rs.getString("alabel"));
				a.setPhonenum(rs.getString("phonenum"));
				a.setProvince(rs.getString("province"));
				a.setStree(rs.getString("stree"));
				a.setUserid(rs.getInt("userid"));
				a.setZipcode(rs.getString("zipcode"));
				a.setZone(rs.getString("zone"));
				a.setZone2(rs.getString("zone2"));
				a.setName(rs.getString("name"));
				list.add(a);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}
	/**
	 * @author Xrh 添加收货地址的方法 添加成功返回i=1。
	 * 
	 */
	public int AddAddress(address a) {
		int i = 0;
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement p = con.prepareStatement("insert into address (userid,name,province,phonenum,zone,zone2,stree,zipcode,alabel) VALUES (?,?,?,?,?,?,?,?,?)");
			p.setInt(1,a.getUserid());
			p.setString(2, a.getName());
			p.setString(3,a.getProvince());
			p.setString(4,a.getPhonenum());
			p.setString(5,a.getZone());
			p.setString(6,a.getZone2());
			p.setString(7,a.getStree());
			p.setString(8,a.getZipcode());
			p.setString(9,a.getAlabel());
			i = p.executeUpdate();
			if(i>1) {
				i = 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}
	/**
	 * @author Xrh 添加收藏夹的方法，添加成功i=1
	 * 
	 */
	public int AddFavorite(int userid , int comid) {
		int i = 0;
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement p = con.prepareStatement("insert into favorite (userid,comid) VALUES (?,?)");
			p.setInt(1,userid);
			p.setInt(2, comid);
			p.execute();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}
	
	/**
	 * @author Xrh 查询用户所有收藏夹。
	 * 
	 */
	public List<commodity> SelevtAllFavorite(int userid){
		List<commodity> list= new ArrayList<>();
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("select * from favorite where userid=?");
			PreparedStatement pstmc = con.prepareStatement("select * from commodity where comid=?");
			pstm.setInt(1, userid);
			ResultSet rs = pstm.executeQuery();
			commodity c = null;
			while (rs.next()) {
				pstmc.setInt(1, rs.getInt("comid"));
				ResultSet rs1 = pstmc.executeQuery();
				while (rs1.next()) {
					c = new commodity();
					c.setComid(rs1.getInt("comid"));
					c.setCategory(rs1.getString("category"));
					c.setCom_name(rs1.getString("com_name"));
					c.setImage(rs1.getString("image"));
					c.setPrice(rs1.getDouble("price"));
					list.add(c);
				}
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public int FavoriteIsExist(int comid, int userid) {
		int i = 0;
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("select comid from favorite where comid =? and userid = ? ");
			pstm.setInt(1, comid);
			pstm.setInt(2, userid);
			ResultSet rs = pstm.executeQuery();
			while(rs.next()){
				i = 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}
	
	/**
	 * @zzx 删除收货地址
	 *
	 */
	public int DeleteAddress(address ad) {
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("delete from address where id=?");
			pstm.setInt(1, ad.getId());
			int rs = pstm.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return 0;
	}
	
	/**
	 * @zzx 修改收货地址
	 *
	 */
	public int UpdateAddress(address ad) {
		try {
			Connection connection = BaceDaoForJDBC.getCon();
			String sql = "update user set userid =?,name =?,province =?,phonenum =?,zone =?,zone2 =?,stree =?,zipcode =?,alabel =? where id = ?";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setInt(1, ad.getUserid());
			ps.setString(2, ad.getName());
			ps.setString(3, ad.getProvince());
			ps.setString(4, ad.getPhonenum());
			ps.setString(5, ad.getZone());
			ps.setString(6, ad.getZone2());
			ps.setString(7, ad.getStree());
			ps.setString(8, ad.getZipcode());
			ps.setString(9, ad.getAlabel());
			ps.setInt(10, ad.getId());
			int rs = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return 0;
	}
	
	
	/**
	 * @zzx 删除收藏夹
	 *
	 */
	public int DeleteFavorite(int comid,int userid) {
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("delete from favorite where comid=? and userid=?");
			pstm.setInt(1, comid);
			pstm.setInt(2, userid);
			int rs = pstm.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return 0;
	}
	
	/**
	 * @zzx 查询user表中用户信息
	 *
	 */
	public user SelectAllByUserId(int userid){
		
		try{
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("select * from user where userid=?");
			pstm.setInt(1, userid);
			ResultSet rs = pstm.executeQuery();
			user u = null;
			while (rs.next()) {
				u = new user();
				u.setUserid(rs.getInt("userid"));
				u.setName(rs.getString("name"));
				u.setUsername(rs.getString("userid"));
				u.setPassword(rs.getString("password"));
				u.setSex(rs.getString("sex"));
			}
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
		
	}
}
